package com.example.linyiquan.application.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.example.linyiquan.application.R;
import com.example.linyiquan.application.adapter.GridAdapter;
import com.example.linyiquan.application.beam.AppBeam;
import com.example.linyiquan.application.util.Utils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * Created by linyiquan on 17-4-20.
 */

public class GridActivity extends Activity {

    private GridView gridView;
    private List<AppBeam> list = new ArrayList<>();
    private GridAdapter gridAdapter;
    private SharedPreferences sp;
    private SharedPreferences.Editor spe;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid);

        sp = getPreferences(0);
        spe = sp.edit();

        initApps();
        initGrid();
    }

    private void initApps() {
        List<AppBeam> apps = Utils.getApps(this);
        Set<String> appPackages = sp.getStringSet("appPackages", new HashSet<String>());
        Iterator iterator = appPackages.iterator();
        while (iterator.hasNext()) {
            String packageName = (String) iterator.next();
            for (int i = 0; i < apps.size(); i++) {
                if (apps.get(i).getPackageInfo().equals(packageName)) {
                    list.add(apps.get(i));
                }
            }
        }
    }

    private void saveData() {
        Set<String> appPackage = new HashSet<>();
        for (int i = 0; i < list.size(); i++) {
            appPackage.add(list.get(i).getPackageInfo());
        }
        spe.putStringSet("appPackages", appPackage);
        spe.commit();
    }

    private void initGrid(){
        gridAdapter=new GridAdapter(GridActivity.this,list);
        gridView= (GridView) findViewById(R.id.gridview);
        gridView.setAdapter(gridAdapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position==gridView.getCount()-2){
                    final List<AppBeam> apps=Utils.getApps(GridActivity.this);
                    final String[] items=new String[apps.size()];
                    for (int i=0;i<apps.size();i++){
                        items[i]=apps.get(i).getAppname();
                    }
                    new AlertDialog.Builder(GridActivity.this)
                            .setTitle("add your app")
                            .setItems(items, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    list.add(list.size()-2,apps.get(which));
                                    gridAdapter.notifyDataSetChanged();
                                    saveData();
                                }
                            }).show();
                }else if (position==gridView.getCount()-1){
                    String[] item=new String[list.size()-2];
                    for (int i=0;i<list.size()-2;i++){
                        item[i]=list.get(i).getAppname();
                    }
                    new AlertDialog.Builder(GridActivity.this)
                            .setTitle("remove your app")
                            .setItems(item, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    list.remove(which);
                                    gridAdapter.notifyDataSetChanged();
                                    saveData();
                                }
                            }).show();
                }else {
                    PackageManager pm=GridActivity.this.getPackageManager();
                    Intent intent=pm.getLaunchIntentForPackage(list.get(position).getPackageInfo());
                    startActivity(intent);
                }
            }
        });
    }

}
